<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display"%>
<%@ taglib uri="/WEB-INF/config/tld/fmt.tld" prefix="fmt" %>
<%@ taglib uri="http://ssh.org/selects" prefix="ssh"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
String applicationPath = basePath + "/" + "application";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="<%=path%>/css/jinmu_style.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="<%=path%>/js/util.js"></script>
<script language="javascript" src="<%=path%>/js/SyncCommon.js"></script>
</head>
<body>
<html:form styleId="yfProjectPlanForm" action="addYfProjectPlan.do?method=addYfProjectPlan" onsubmit="return save();">
	<input type ="hidden" name="projectid" id="projectid" value="-1"/>
	<input type ="hidden" name="id" id="id" value="${yfProjectPlanForm.id}"/>
	  <input name='lastIndexPlan' type='hidden' id='lastIndexPlan' value="1" />
	<table cellspacing="0" cellpadding="0" width="98%" align="center"  border="0" class="margtb5">
		    <tr>
		      <td align="left" class="padl5 lan12"><img src="<%=path%>/images/img-11.gif" width="12" height="12"> 项目管理 &gt; 项目计划 &gt; 新增</td>
		    </tr>
       </table>
	
	   
	    <table cellspacing="1" cellpadding="0" width="98%" align="center"  border="0" class="bgcolor2 margb5">
	          <tr>
		       <td colspan="4" align="left" class="bkuang zi13b bgcolor2 padl5">项目基本信息</td>
		    </tr>
		     <tr class="bgcolor">

 
	   <td width="18%" align="right" class="zi13">计划类型：</td>
	   <td width="32%"  align="left" class="pad2 zi13" colspan="3">
	    	<ssh:select propertyName="planType" pid="PLAN_TYPE" type="1" firstShow="N" onChangeValue="showDiv();"
							defaultName="${yfProjectPlanForm.planType}" />
	                       		  
	  </td>
	   
		    
        </tr> 
		         <tr class="bgcolor">
	   <td width="18%" align="right" class="zi13">项目名称：<font color="red">*&nbsp;</font></td>
	   <td width="32%"  align="left" class="pad2 zi13">
	                               				 <input type="text"  flag="true"  maxlength="60"  valid="required" errMsg="请输入项目名称！"  id="projectName" name="projectName" readonly="true"/>
	                               				 <input type="button" onclick="chooseProject();" name="choose" value="选择" class="buttonbg" style="cursor:hand">
		         		                       		  
	  </td>

 
	   <td width="18%" align="right" class="zi13">项目编号：</td>
	   <td width="32%"  align="left" class="pad2 zi13">
	                               				 <input type="text" readonly="true" id="projectNo" name="projectNo"/>
		         		                       		  
	  </td>

        </tr> 
             <tr class="bgcolor">

 
	   <td width="18%" align="right" class="zi13">项目机构：</td>
	   <td width="32%"  align="left" class="pad2 zi13">
	                               			 <ssh:select readonly="true" id="projectOrg" propertyName="projectOrg" firstShow="Y" dataSource="collection" selectList="${orgSelect}"  type="1" defaultName="${projectOrg}"/>
	                       		  
	  </td>
	   <td width="18%" align="right" class="zi13"><bean:message key="com.application.yfprojectplan.entity.yfprojectplan.createpage.projectManager" bundle="yfprojectplan"/>：</td>
	   <td width="32%"  align="left" class="pad2 zi13">
	                                              <input type="text" readonly="true" id="rname" name="rname" value="${yfProjectPlanForm.rname}"/>
	                               				 <input type="hidden" id="projectManager" name="projectManager" value="${yfProjectPlanForm.projectManager}"/>
		         		                       		  
	  </td>
        </tr> 
         
 
 
 
  <tr class="bgcolor" style="display:none;" id="bg1">

 
	   <td width="18%" align="right" class="zi13">变更理由：</td>
	   <td width="32%"  align="left" class="pad2 zi13" colspan="3">
	                               			<textarea rows="8" cols="70" flag="true"  valid="limitLength" max="200" errMsg="变更理由输入长度不能超过200个汉字" id="changeReson" name="changeReson"></textarea>长度不超过200个字
	                       		  
	  </td>
	   
		    
        </tr> 
        
        
     <tr class="bgcolor" style="display:none;" id="bg2">

 
	   <td width="18%" align="right" class="zi13">变更计划：</td>
	   <td width="32%"  align="left" class="pad2 zi13" colspan="3">
	                               			<textarea rows="8" cols="70" flag="true"  valid="limitLength" max="200" errMsg="变更计划输入长度不能超过200个汉字"  id="changePlan" name="changePlan"></textarea>长度不超过200个字
	                       		  
	  </td>
	   
		    
        </tr> 
        
        
          <tr class="bgcolor" style="display:none;" id="bg3">
	   <td width="18%" align="right" class="zi13">项目完成时间：<font color="red">*&nbsp;</font></td>
	   <td width="32%"  align="left" class="pad2 zi13">
	                               			 <input flag="false"   valid="required" errMsg="请输入项目完成时间！" type="text"    name="changeCdateStr" id="changeCdateStr" readonly="true"  onclick="popUpCalendarDlg(this);getDayInfo();" />
	                               			
		         		                       		  
	  </td>

 
	   <td width="18%" align="right" class="zi13">变更类型：</td>
	   <td width="32%"  align="left" class="pad2 zi13">
	         <ssh:select propertyName="bgType" id="bgType" pid="BIANGG_TYPE" type="1" firstShow="N"  
							defaultName="${yfProjectPlanForm.bgType}" />	&nbsp;&nbsp;偏离时间：<font color="red">*&nbsp;</font> <input type="text" flag="false"  maxlength="6" valid="required|isMoney" errMsg="请输入偏离时间！|请输入正确的偏离时间！"  size="5" id="delayDay" name="delayDay" />天
		         		                       		  
	  </td>

        </tr> 
 
 
 
 
 
 
 

    </table> 
    
    <div id="yjh">
      <table cellspacing="1" cellpadding="0" width="98%" align="center"  border="0" class="bgcolor2 margb5">
	          <tr>
		       <td colspan="4" align="left" class="bkuang zi13b bgcolor2 padl5">计划内容</td>
		       <td align="right"><input class="buttonbg" style="cursor:hand;" id="but_save2" type="button" value="增加计划内容" onclick="insertPlanRow()"   name="but_save2" /></td>
		    </tr>
		         <tr class="bgcolor">
	
	            <td width="32%"  align="left" class="pad2 zi13"  colspan="5">
	            
	             <table cellspacing="1" id="planTable" cellpadding="0" width="100%" align="center"  border="0" class="bgcolor2 margb5">
                 </table>
             
	             </td>


        </tr> 
        
 

    </table> 
     <table cellspacing="1" cellpadding="0" width="98%" align="center"  border="0" class="bgcolor2 margb5">
	<tr>
					    <td colspan="4">
		                    <iframe width="100%" height="150px" onload="this.height=show_attachment.document.body.scrollHeight"
							src="<%=path%>/queryAttachment.do?method=queryAttachment&fid=${yfProjectPlanForm.id}&type=10004"
											; scrolling="no" frameborder="0" name="show_attachment"
											id="show_attachment"></iframe>       
					    </td>
					  </tr>		
</table>
</div>










		<p><p><p>
	
       <table width="98%" align="center" border="0" cellspacing="0" cellpadding="0">
  <tr>
        <td align="center">
		<input class="buttonbg" id="but_save" style="cursor:hand" type="submit" value="<bean:message key='com.manager.common.yfprojectplan.save' bundle="yfprojectplan"/>" name="but_save" />
		  &nbsp;&nbsp;&nbsp;
		  		<input class="buttonbg" id="but_save2" style="cursor:hand" type="button" value="提交审核" onclick="return returnSubmits()"  name="but_save2" />
						&nbsp;&nbsp;&nbsp;
	     <input class="buttonbg" id="but_return"  style="cursor:hand"  type="button" value="<bean:message key='com.manager.common.yfprojectplan.return' bundle="yfprojectplan"/>" name="but_return" onClick="javascript:history.go(-1);"/>
		 </td>
  </tr>
</table>

</html:form>
</body>
</html>


<script>

var maxDate =  '${yfProjectPlan.changeCdateStr}';
function getDayInfo(){
    $('delayDay').value = '';
   // checkDiffDays();
}

//提交审核
function returnSubmits(){
  var planType = $('planType').value;
    if(validateForm(document.forms[0],'Y'))
	{
	 
	 if("2" == planType){
   	      var changeCdateStr = $('changeCdateStr').value;  
   	      if(changeCdateStr == null || changeCdateStr ==""){
   	           alert("请输入项目完成时间！");
	            return false;
   	      }
   	       var delayDay = $('delayDay').value;  
   	      if(delayDay == null || delayDay ==""){
   	           alert("请输入偏离时间！");
	            return false;
   	      }
   	      
      }
      if("1" == planType){
	      if(!checkPlan()){
	   	    alert("请增加计划内容！");
		    return false;
		 }
      }
	 if(confirm("是否提交审核？") ){
	    document.forms[0].action="submitWorkFlowYfProjectPlan.do?method=submitWorkFlowYfProjectPlan&submitType=add";
	    document.forms[0].submit();
		return true;
	  }
	  return false;
		
	}else{
	    return false;
	}
	
}

function checkPlan(){
	 var lastIndex = $('lastIndexPlan').value;
	 if(lastIndex<=1){
	 	return false;
	 }
	 return true;
}

function save(){
   var planType = $('planType').value;

    
   if(validateForm(document.forms[0],'Y'))
   {
      if("2" == planType){
   	      var changeCdateStr = $('changeCdateStr').value;  
   	      if(changeCdateStr == null || changeCdateStr ==""){
   	           alert("请输入项目完成时间！");
	            return false;
   	      }
   	       var delayDay = $('delayDay').value;  
   	      if(delayDay == null || delayDay ==""){
   	           alert("请输入偏离时间！");
	            return false;
   	      }
      }
      if("1" == planType){
	      if(!checkPlan()){
	   	    alert("请增加计划内容！");
		    return false;
		 }
      }
   	 
	return true;
    }else{
	return false;
    }
}

function chooseProject(){   
    var syncHelper = new SyncHelper();
    var planType = $('planType').value;
    var url = "popuSelectProjectList.do?method=popuSelectProjectList&projectManager=${loginUser.id}&random=" + new Date();
 
    if("2" == planType){
   	  url = "popuSelectProjectList.do?method=popuSelectProjectList&projectManager=${loginUser.id}&queryType=2&random=" + new Date();
    }
	var searchWin = window.showModalDialog(url,window,"help:no;scroll:yes;resizable:no;status:0;dialogWidth:1000px;dialogHeight:600px;center:yes" ); 	
    if(searchWin){
         $("projectName").value = searchWin["projectName"]==undefined?"":searchWin["projectName"];
		 $("projectNo").value = searchWin["projectNo"]==undefined?"":searchWin["projectNo"];
		 syncHelper.selectedByValue(searchWin["projectOrg"],"projectOrg");
		 $("projectid").value = searchWin["projectNo"]==undefined?"":searchWin["projectNo"];
		 //$jq("#projectOrg option").not(":selected").attr("disabled", "disabled");

		 
    }
}

function insertPlanRow(){
    var lastIndex = $('lastIndexPlan').value;
    var addrow = $('planTable').insertRow();
  	addrow.className="bgcolor";
    var addcellone = addrow.insertCell();
    addcellone.className="zi13";
    addcellone.setAttribute("align","right");
    addrow.setAttribute("id","tr"+lastIndex);  
	addcellone.innerHTML = '时间:';
	addcellone = addrow.insertCell();
	addcellone.className="pad2 zi13";
	addcellone.setAttribute("align","left");
	addcellone.colSpan =3;
	 
	addcellone.innerHTML = "<input type='text' readonly='true' valid='required' flag='true'  errMsg='请输入计划开始时间！' id='plan"+lastIndex+"*startDateStr'  name='plan"+lastIndex+"*startDateStr' onclick='popUpCalendarDlg(this)' />  至   <input type='text' readonly='true' valid='required' flag='true'  errMsg='请输入计划结束时间！' id='plan"+lastIndex+"*endDateStr'  name='plan"+lastIndex+"*endDateStr' onclick='popUpCalendarDlg(this)' />";
	var addrow = $('planTable').insertRow();
	addrow.className="bgcolor";
	var addcellone = addrow.insertCell();
    addcellone.className="zi13";
    addcellone.setAttribute("align","right");
    addrow.setAttribute("id","tr"+lastIndex);  
	addcellone.innerHTML = '计划内容:';
	addcellone = addrow.insertCell();
	addcellone.colSpan =3;
	addcellone.className="pad2 zi13";
	addcellone.setAttribute("align","left");
	addcellone.innerHTML = "<textarea cols='70' rows='5' flag='true' valid='required|limitLength' max='200'  errMsg='请输入计划内容！|'计划内容输入长度不能超过200个汉字' name='plan"+ lastIndex +"*planContent' id='plan"+ lastIndex +"*planContent'></textarea><a href='#' onclick='delPlanRow("+lastIndex+")'>&nbsp;&nbsp;删除</a>";
	$('lastIndexPlan').value = parseInt(lastIndex) + 1;
}

function delPlanRow(index){
   var table = $('planTable');
   var row = $('tr' + index);
   var rowIndex = row.rowIndex;
   
   table.deleteRow(rowIndex); 
   var table = $('planTable');
   var row = $('tr' + index);
   var rowIndex = row.rowIndex;

   table.deleteRow(rowIndex); 
   var temp =  table.rows.length;
   $('lastIndexPlan').value = table.rows.length;
}

function showDiv(){
   var planType = $('planType').value;
   if("1" == planType){
   	    $('bg1').style.display = "none";
   	    $('bg2').style.display = "none";
   	    $('bg3').style.display = "none";
   	    $('yjh').style.display = "block";
   	   
   	    
   }
   if("2" == planType){
   	    $('bg1').style.display = "block";
   	    $('bg2').style.display = "block";
   	    $('bg3').style.display = "block";
   	    $('yjh').style.display = "none";
   }
}


//计算两个日期的差值
function checkDiffDays() {
    var gmtEndDay = document.getElementById("changeCdateStr").value;
    var diffDays = getDiffDays(maxDate,gmtEndDay);

     
    if(diffDays==undefined || diffDays =='NaN' || isNaN(diffDays)){
    	alert("项目完成时间必须大于" + maxDate);
    	return false;
    	//diffDays = getDiffDays(gmtEndDay,maxDate);
    	//diffDays = 0-diffDays;

    }
    $('delayDay').value = diffDays;
    return true;
}


//得到两个日期的间隔时间的天数
function getDiffDays(date1,date2) {
    var regexp=/^(\d{1,4})[-|\.]{1}(\d{1,2})[-|\.]{1}(\d{1,2})$/;
    var monthDays=[0,3,0,1,0,1,0,0,1,0,0,1];
    regexp.test(date1);
    var date1Year=RegExp.$1;
    var date1Month=RegExp.$2;
    var date1Day=RegExp.$3;

    regexp.test(date2);
    var date2Year=RegExp.$1;
    var date2Month=RegExp.$2;
    var date2Day=RegExp.$3;
	
	if (validatePeriod(date1Year,date1Month, date1Day, date2Year, date2Month, date2Day)) {
		var firstDate = new Date(date1Year, date1Month, date1Day);
		var secondDate = new Date(date2Year, date2Month, date2Day);
		
		var result = Math.floor((secondDate.getTime() - firstDate.getTime()) / (1000*3600*24));
		for(j = date1Year; j < date2Year; j++) {
			if (isLeapYear(j)) {
				monthDays[1] = 2;
			} else {
				monthDays[1] = 3;
			}
			for(i = date1Month -1 ; i < date2Month; i++) {
				result = result - monthDays[i]
			}
		}
		return result;
	} else {
		return ;
	}
	if(validatePeriod(date1Year,date1Month,date1Day,date2Year,date2Month,date2Day)){
    	firstDate=new Date(date1Year,date1Month,date1Day);
         secondDate=new Date(date2Year,date2Month,date2Day);
      
         result=Math.floor((secondDate.getTime()-firstDate.getTime())/(1000*3600*24));
         for(j=date1Year;j<=date2Year;j++){
             if(isLeapYear(j)){
                 monthDays[1]=2;
             }else{
                 monthDays[1]=3;   
             }
             for(i=date1Month-1;i<date2Month;i++){
                 result=result-monthDays[i];
             }
         }
         return result;
     }else{
      return 'the first field must before the second date.';
     }
}

//判断年份是否是闰年
function isLeapYear(year) {
	if (year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0)) {
		return true;
	}
	return false;
}

//判断前后两个日期
function validatePeriod(fyear, fmonth, fday, byear, bmonth, bday) {
	if (fyear < byear) {
		return true;
	} else if (fyear == byear) {
		if (fmonth < bmonth) {
			return true;
		} else if (fmonth == bmonth) {
			if (fday <= bday) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

</script>





